package com.im.partchat.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.im.partchat.App;
import com.im.partchat.R;
import com.im.partchat.adapter.NewFriendListAdapter;
import com.im.partchat.base.BaseActivity;
import com.im.partchat.bean.FriendApply;
import com.im.partchat.db.dao.UserDao;
import com.im.partchat.mvp.persenter.FriendPresenter;

import java.util.List;

import butterknife.BindView;

/**
 * Created by hhz on 2017/7/3.
 */
public class NewFriendListActivity extends BaseActivity implements NewFriendListAdapter.OnItemButtonClick {
    @BindView(R.id.isData)
    TextView isData;
    @BindView(R.id.shiplistview)
    ListView shiplistview;

    private NewFriendListAdapter adapter;
    private FriendPresenter presenter;
    private int index;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_new_friendlist;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        adapter = new NewFriendListAdapter(this);
        shiplistview.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        presenter = new FriendPresenter(this, this);
        presenter.friendApplyList(App.getUserBean().getId());
    }

    @Override
    public void onLoadDataSuccess(Object o, String flag) {
        if ("friendApplyList".equals(flag)) {
            if (o != null) {
                List<FriendApply> applies = (List<FriendApply>) o;
                adapter.removeAll();
                adapter.addData(applies);
                adapter.notifyDataSetChanged();
                adapter.setOnItemButtonClick(this);
            }
        } else if ("friendApplyConfirm".equals(flag)) {
            UserDao.getInstance().updateUser(((List<FriendApply>)adapter.getDataSet()).get(index).getApplyUserId());
            presenter.friendApplyList(App.getUserBean().getId());
        }
    }

    @Override
    public boolean onButtonClick(int position, View view, int status) {
        index = position;
        switch (status) {
            case 1: //收到了好友邀请
                presenter.friendApplyConfirm(((List<FriendApply>)adapter.getDataSet()).get(position).getId(), true);
                break;
            case 2:
                break;
            case 3:
                break;
        }
        return false;
    }
}
